package com.gkd.config;

import com.gkd.interceptor.JwtTokenAdminInterceptor;
import com.gkd.interceptor.JwtTokenRegularInterceptor;
import com.gkd.properties.UploadProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

@Configuration
@Slf4j
public class MvcConfig extends WebMvcConfigurationSupport {
    @Autowired
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
    @Autowired
    private JwtTokenRegularInterceptor jwtTokenRegularInterceptor;

    @Autowired
    private UploadProperties uploadProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册拦截器");
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/user/login");
        registry.addInterceptor(jwtTokenRegularInterceptor)
                .addPathPatterns("/regular/**")
                .excludePathPatterns("/regular/user/login","/regular/user/register","/regular/post/page","/regular/category");
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开启文件路径映射："+new File(uploadProperties.getDirectory()).getAbsolutePath()+"/");
        registry.addResourceHandler("/upload/**")
                .addResourceLocations("file:"+new File(uploadProperties.getDirectory()).getAbsolutePath()+"/");
    }
}
